/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.csource.manage.enums.CaseSourceEnum;
import com.icinfo.cloud.provider.punish.csource.manage.enums.CaseSourceStatusEnum;
import com.icinfo.cloud.provider.punish.csource.manage.mapper.CaseSourceDocumentRecordMapper;
import com.icinfo.cloud.provider.punish.csource.manage.mapper.CaseSourceRegisterMainInfoMapper;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceDocumentRecord;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceRegisterMainInfo;
import com.icinfo.cloud.provider.punish.csource.manage.service.ICaseSourcePartyInfoService;
import com.icinfo.cloud.provider.punish.csource.manage.service.ICaseSourceRegisterMainInfoService;
import com.icinfo.cloud.provider.punish.csource.manage.vo.CaseSourceBasicInfoVo;
import com.icinfo.cloud.provider.punish.csource.manage.vo.CaseSourcePartyInfoVo;
import com.icinfo.cloud.provider.punish.evideinv.service.EvidenceInventoryService;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseDocumentRecordCriminalDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.CaseStageEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.CaseStatusEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CasePartiesLegalInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CasePartiesPersonInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CasePartiesLegalInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CasePartiesPersonInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICasePartiesRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseReasonMainRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CasePartiesRecordVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseReasonMainRecordVo;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.dto.CaseTransferIdTypeDto;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.dto.CaseTransferRecordDto;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.mapper.CaseTransferRecordFileMapper;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.mapper.CaseTransferRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.model.CaseTransferRecord;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.model.CaseTransferRecordFile;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.service.ICaseTransferRecordService;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.vo.CaseInitiateInfoVo;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.vo.CasePartyVo;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.vo.CaseReasonVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述: 案件移交记录表 case_transfer_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月08日
 */
@Service
public class CaseTransferRecordServiceImpl extends MyBatisServiceSupport implements ICaseTransferRecordService {

    @Resource
    private CaseTransferRecordMapper caseTransferRecordMapper;
    @Resource
    private CaseMainInfoMapper caseMainInfoMapper;
    @Resource
    private CaseTransferRecordFileMapper caseTransferRecordFileMapper;
    @Resource
    private ICasePartiesRecordService casePartiesRecordService;
    @Resource
    private CasePartiesPersonInfoMapper casePartiesPersonInfoMapper;
    @Resource
    private CasePartiesLegalInfoMapper casePartiesLegalInfoMapper;
    @Resource
    private ICaseReasonMainRecordService caseReasonMainRecordService;
    @Resource
    private ICaseSourceRegisterMainInfoService caseSourceRegisterMainInfoService;
    @Resource
    private ICaseSourcePartyInfoService caseSourcePartyInfoService;
    @Resource
    private CaseSourceRegisterMainInfoMapper caseSourceRegisterMainInfoMapper;
    @Resource
    private EvidenceInventoryService evidenceInventoryService;
    @Resource
    private CaseSourceDocumentRecordMapper caseSourceDocumentRecordMapper;
    @Resource
    private ICaseDocumentRecordService caseDocumentRecordService;

    /**
     * 修改移交记录
     *
     * @param caseTransferRecordDto 案件移交记录入参对象
     * @return com.icinfo.framework.common.web.Result<java.lang.String>
     * @author liuyongfei
     * @date 2022/12/08
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> updateCaseTransferRecord(CaseTransferRecordDto caseTransferRecordDto) {
        if (StringUtils.isBlank(caseTransferRecordDto.getCaseId()) && StringUtils.isBlank(caseTransferRecordDto.getCaseSourceId())) {
            return Result.error("请传入案源id或案件id");
        }

        caseTransferRecordDto.setUpdated(new Date());

        Example example = buildExample(caseTransferRecordDto);

        if (!Constant.STRING_1.equals(caseTransferRecordDto.getTransferType())) {

            if (CaseStatusEnum.TRANSFERRING.getDes().equals(caseTransferRecordDto.getStatus())) {
                //审批完成
                caseTransferRecordDto.setApprovalFinishTime(new Date());

                //更新案件主表状态为已移送
                if (StringUtils.isNotBlank(caseTransferRecordDto.getCaseId())) {
                    updateCaseStatus(caseTransferRecordDto.getCaseId(), CaseStatusEnum.TRANSFERRING.getCode());
                } else {
                    updateCaseSourceStatus(caseTransferRecordDto.getCaseSourceId(), CaseSourceStatusEnum.TRANSFER.getIndex());
                }
            } else if (Constant.STRING_CLOSED.equals(caseTransferRecordDto.getStatus())) {
                //删除文件记录，以便重新移交
                Example fileExample = new Example(CaseTransferRecordFile.class);
                fileExample.createCriteria().andEqualTo("sendUid", caseTransferRecordDto.getSendUid());
                CaseTransferRecordFile caseTransferRecordFile = new CaseTransferRecordFile();
                caseTransferRecordFile.setUpdated(new Date());
                caseTransferRecordFile.setDeleted(Constant.STRING_1);
                caseTransferRecordFile.setSendUid(null);
                caseTransferRecordFileMapper.updateByExample(caseTransferRecordFile, fileExample);
            }

        }

        if (Constant.STRING_1.equals(caseTransferRecordDto.getTransferType()) && "已反馈".equals(caseTransferRecordDto.getStatus())) {
            caseTransferRecordDto.setApprovalFinishTime(new Date());
        }

        if (Constant.STRING_1.equals(caseTransferRecordDto.getTransferType()) && CollectionUtils.isNotEmpty(
                caseTransferRecordDto.getFileInfoList())) {
            List<Dict> list = new ArrayList<>();
            caseTransferRecordDto.getFileInfoList().forEach(a -> {
                Dict dict = Dict.create();
                dict.put("fileName", a.getFileName());
                dict.put("fileUrl", a.getFileUrl());
                dict.put("createTime", a.getCreateTime());
                dict.put("fileDesc", a.getFileDesc());
                list.add(dict);
            });
            caseTransferRecordDto.setFileUrl(JSONUtil.toJsonStr(list));
        }

        boolean isSuccess = caseTransferRecordMapper.updateByExampleSelective(caseTransferRecordDto, example) > 0;
        return isSuccess ? Result.success("修改成功！") : Result.error("修改失败！");
    }

    /**
     * 新增案件移交记录
     *
     * @param caseTransferRecordDto 案件移交修改参数
     * @return com.icinfo.framework.common.web.Result<java.lang.String>
     * @author liuyongfei
     * @date 2022/12/08
     */
    @Override
    public Result<String> saveCaseTransferRecord(CaseTransferRecordDto caseTransferRecordDto) {
        if (StringUtils.isBlank(caseTransferRecordDto.getCaseId()) && StringUtils.isBlank(caseTransferRecordDto.getCaseSourceId())) {
            return Result.error("请传入案源id或案件id");
        }

        //校验移交记录的唯一
        Example example = buildExample(caseTransferRecordDto);
        example.createCriteria().andEqualTo("deleted", Constant.STRING_0)
                .andEqualTo("sendUid", caseTransferRecordDto.getSendUid())
                .andNotEqualTo("status", Constant.STRING_CLOSED);
        List<CaseTransferRecord> caseTransferRecords = caseTransferRecordMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(caseTransferRecords)) {
            return Result.error("请勿重复移交！");
        }

        Example fileExample = new Example(CaseTransferRecordFile.class);

        //UserDTO userInfo = UserUtil.getUserInfo();
        //assert userInfo != null;
        caseTransferRecordDto.setId(StringUtils.uuid());
        caseTransferRecordDto.setCreated(new Date());
        caseTransferRecordDto.setUpdated(new Date());
        //如果为案件移交 更新案件状态为：移送中
        if (Constant.STRING_2.equals(caseTransferRecordDto.getTransferType())) {
            fileExample.createCriteria().andEqualTo("caseId", caseTransferRecordDto.getCaseId());
            //获取当前阶段编码
            CaseMainInfo caseMainInfo = getCaseMainInfo(caseTransferRecordDto.getCaseId());
            if (Objects.nonNull(caseMainInfo)) {
                caseTransferRecordDto.setStageCode(caseMainInfo.getCaseCurrentStageCode());
            }
        } else if (Constant.STRING_3.equals(caseTransferRecordDto.getTransferType())) {
            fileExample.createCriteria().andEqualTo("caseSourceId", caseTransferRecordDto.getCaseSourceId());

            CaseSourceRegisterMainInfo caseSourceRegisterMainInfo =
                    caseSourceRegisterMainInfoMapper.selectByPrimaryKey(caseTransferRecordDto.getCaseSourceId());
            caseTransferRecordDto.setStageCode(caseSourceRegisterMainInfo.getCaseSourceStageCode());
        }
        boolean isSuccess = caseTransferRecordMapper.insertSelective(caseTransferRecordDto) > 0;

        //保存sendUid到移送附件表
        CaseTransferRecordFile caseTransferRecordFile = new CaseTransferRecordFile();
        caseTransferRecordFile.setSendUid(caseTransferRecordDto.getSendUid());
        caseTransferRecordFileMapper.updateByExampleSelective(caseTransferRecordFile, fileExample);

        return isSuccess ? Result.success("新增成功！") : Result.error("新增失败！");
    }

    /**
     * 删除案件移交记录
     *
     * @param caseTransferIdTypeDto 案件移交删除参数对象
     * @return java.lang.Boolean
     * @author liuyongfei
     * @date 2022/12/09
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> deleteCaseTransferRecord(CaseTransferIdTypeDto caseTransferIdTypeDto) {
        if (StringUtils.isBlank(caseTransferIdTypeDto.getCaseId()) && StringUtils.isBlank(caseTransferIdTypeDto.getCaseSourceId())) {
            return Result.error("请传入案源id或案件id");
        }

        //删除选择的移交材料记录
        Example recordFileExample = new Example(CaseTransferRecordFile.class);
        Example recordExample = new Example(CaseTransferRecord.class);

        if (Constant.STRING_3.equals(caseTransferIdTypeDto.getTransferType())) {
            recordFileExample.createCriteria().andEqualTo("caseSourceId", caseTransferIdTypeDto.getCaseSourceId())
                    .andEqualTo("sendUid", caseTransferIdTypeDto.getSendUid());
            recordExample.createCriteria().andEqualTo("caseSourceId", caseTransferIdTypeDto.getCaseSourceId())
                    .andEqualTo("transferType", caseTransferIdTypeDto.getTransferType())
                    .andEqualTo("sendUid", caseTransferIdTypeDto.getSendUid());
        } else {
            recordFileExample.createCriteria().andEqualTo("caseId", caseTransferIdTypeDto.getCaseId())
                    .andEqualTo("sendUid", caseTransferIdTypeDto.getSendUid());
            recordExample.createCriteria().andEqualTo("caseId", caseTransferIdTypeDto.getCaseId())
                    .andEqualTo("transferType", caseTransferIdTypeDto.getTransferType())
                    .andEqualTo("sendUid", caseTransferIdTypeDto.getSendUid());
        }
        CaseTransferRecordFile caseTransferRecordFile = new CaseTransferRecordFile();
        caseTransferRecordFile.setUpdated(new Date());
        //逻辑删除
        caseTransferRecordFile.setDeleted(Constant.STRING_1);
        caseTransferRecordFile.setSendUid(null);

        caseTransferRecordFileMapper.updateByExample(caseTransferRecordFile, recordFileExample);

        CaseTransferRecord caseTransferRecord = new CaseTransferRecord();
        caseTransferRecord.setUpdated(new Date());
        caseTransferRecord.setDeleted(Constant.STRING_1);

        //删除案件移交记录
        boolean isSuccess = caseTransferRecordMapper.updateByExampleSelective(caseTransferRecord, recordExample) > 0;

        return isSuccess ? Result.success("删除成功！") : Result.error("删除失败！");
    }

    /**
     * 查询案件移交发起信息
     *
     * @param caseTransferIdTypeDto 案件移交参数对象
     * @return com.icinfo.framework.common.web.Result<java.lang.String>
     * @author liuyongfei
     * @date 2022/12/09
     */
    @Override
    public Result<CaseInitiateInfoVo> getInitiateInfo(CaseTransferIdTypeDto caseTransferIdTypeDto) {
        if (StringUtils.isBlank(caseTransferIdTypeDto.getCaseId()) && StringUtils.isBlank(caseTransferIdTypeDto.getCaseSourceId())) {
            return Result.error("请传入案源id或案件id");
        }

        //案件信息
        CaseInitiateInfoVo caseInitiateInfoVo = new CaseInitiateInfoVo();

        Example example = new Example(CaseTransferRecordFile.class);

        //案件基本信息
        String id;

        if (Constant.STRING_1.equals(caseTransferIdTypeDto.getTransferType())
                || Constant.STRING_2.equals(caseTransferIdTypeDto.getTransferType())) {

            id = caseTransferIdTypeDto.getCaseId();

            example.createCriteria().andEqualTo("caseId", caseTransferIdTypeDto.getCaseId())
                    .andEqualTo("transferType", Constant.STRING_2).andEqualTo("deleted", Constant.STRING_0);

            CaseMainInfo caseMainInfo = getCaseMainInfo(caseTransferIdTypeDto.getCaseId());
            if (Objects.nonNull(caseMainInfo)) {
                BeanUtil.copyProperties(caseMainInfo, caseInitiateInfoVo);
            }

            caseInitiateInfoVo.setCaseId(caseTransferIdTypeDto.getCaseId());
            //案由信息
            List<CaseReasonMainRecordVo> caseReasonRecordList =
                    caseReasonMainRecordService.getCaseReasonRecordListByCaseId(caseTransferIdTypeDto.getCaseId());

            List<CaseReasonVo> caseReasonVoList = new ArrayList<>();

            caseReasonRecordList.forEach(caseReasonMainRecordVo -> {
                CaseReasonVo caseReasonVo = new CaseReasonVo();
                caseReasonVo.setCaseReasonCode(caseReasonMainRecordVo.getCaseReasonCode());
                caseReasonVo.setCaseReasonName(caseReasonMainRecordVo.getCaseReasonName());
                caseReasonVoList.add(caseReasonVo);
            });
            caseInitiateInfoVo.setCaseReasonVoList(caseReasonVoList);

            if (Constant.STRING_1.equals(caseTransferIdTypeDto.getIsGetAllFiles())) {
                //案件移交文书及证据
                List<CaseTransferRecordFile> allCaseTransferRecordFileList = getAllCaseTransferRecordFileList(caseTransferIdTypeDto.getTransferType(), caseTransferIdTypeDto.getCaseId());
                caseInitiateInfoVo.setCaseTransferRecordFileList(allCaseTransferRecordFileList);
            }

        } else {

            id = caseTransferIdTypeDto.getCaseSourceId();

            example.createCriteria().andEqualTo("caseSourceId", caseTransferIdTypeDto.getCaseSourceId())
                    .andEqualTo("transferType", Constant.STRING_3).andEqualTo("deleted", Constant.STRING_0);
            //案源信息

            CaseSourceBasicInfoVo caseSourceBasicInfoVo =
                    caseSourceRegisterMainInfoService.getCaseSourceBasicInfoVo(caseTransferIdTypeDto.getCaseSourceId());
            caseInitiateInfoVo.setCaseSourceId(caseTransferIdTypeDto.getCaseSourceId());
            caseInitiateInfoVo.setCaseName(caseSourceBasicInfoVo.getCaseSourceName());
            caseInitiateInfoVo.setCaseNumber(caseSourceBasicInfoVo.getCaseSourceNumber());
            caseInitiateInfoVo.setCaseParties(caseSourceBasicInfoVo.getPartyName());
            caseInitiateInfoVo.setCaseSource(caseSourceBasicInfoVo.getCaseSource());
            caseInitiateInfoVo.setCaseSource(CaseSourceEnum.getValueByCode(caseSourceBasicInfoVo.getCaseSource()));
            caseInitiateInfoVo.setHandlePersonMainName(caseSourceBasicInfoVo.getHandlePersonMainName());
            caseInitiateInfoVo.setCaseCreateTime(caseSourceBasicInfoVo.getRegisterTime());

            if (Constant.STRING_1.equals(caseTransferIdTypeDto.getIsGetAllFiles())) {
                //案源移交文书及证据
                List<CaseTransferRecordFile> allCaseTransferRecordFileList = getAllCaseTransferRecordFileList(caseTransferIdTypeDto.getTransferType(), caseTransferIdTypeDto.getCaseSourceId());
                caseInitiateInfoVo.setCaseTransferRecordFileList(allCaseTransferRecordFileList);
            }
        }

        List<CasePartyVo> casePartyVoList = getCasePartyVoList(caseTransferIdTypeDto.getTransferType(), id);
        caseInitiateInfoVo.setCasePartyVoList(casePartyVoList);

        if (StringUtils.isBlank(caseInitiateInfoVo.getCaseParties()) && CollectionUtils.isNotEmpty(casePartyVoList)) {
            List<String> partyList = casePartyVoList.stream().map(CasePartyVo::getPartyName).collect(Collectors.toList());
            String parties = StringUtils.join(partyList, ",");
            caseInitiateInfoVo.setCaseParties(parties);
        }

        if (Constant.STRING_1.equals(caseTransferIdTypeDto.getTransferType()) || !Constant.STRING_1.equals(caseTransferIdTypeDto.getIsGetAllFiles())) {
            //协查协办走原来逻辑 或 不是获取所有文件
            //移交文书及证据
            List<CaseTransferRecordFile> caseTransferRecordFileList = caseTransferRecordFileMapper.selectByExample(example);
            caseInitiateInfoVo.setCaseTransferRecordFileList(caseTransferRecordFileList);
        }

        return Result.success(caseInitiateInfoVo);
    }

    /**
     * 获取所有的移交文书及证据
     * @param transferType
     * @param caseId
     * @return
     */
    public List<CaseTransferRecordFile> getAllCaseTransferRecordFileList (String transferType, String caseId) {
        List<CaseTransferRecordFile> caseTransferRecordFileList = new ArrayList<>();

        //案件移交
        if (Constant.STRING_2.equals(transferType)) {
            //案件文书
            List<CaseDocumentRecordCriminalDto> materialVoList = caseDocumentRecordService.getTransferDocumentRecordOfCriminal(caseId);
            if (ObjectUtil.isNotEmpty(materialVoList)) {
                for (CaseDocumentRecordCriminalDto caseDocumentRecordCriminalDto : materialVoList) {
                    CaseTransferRecordFile caseTransferRecordFile = new CaseTransferRecordFile();
                    caseTransferRecordFile.setCaseSourceId(caseId);
                    caseTransferRecordFile.setFileName(caseDocumentRecordCriminalDto.getDocumentNumber());
                    caseTransferRecordFile.setFileUrl(caseDocumentRecordCriminalDto.getDocumentUrl());
                    caseTransferRecordFile.setTransferType(Constant.STRING_2);
                    caseTransferRecordFile.setEvidenceCata(Constant.STRING_2);
                    caseTransferRecordFile.setFileType("pdf");
                    caseTransferRecordFile.setFileSuffix(".pdf");
                    caseTransferRecordFileList.add(caseTransferRecordFile);
                }
            }
        }

        //案源移交
        if (Constant.STRING_3.equals(transferType)) {
            //案源文书
            Example example = new Example(CaseSourceDocumentRecord.class);
            example.createCriteria().andEqualTo("caseSourceId", caseId).andEqualTo("documentRecordStatus",
                    Constant.STRING_1);
            List<CaseSourceDocumentRecord> caseSourceDocumentRecordList = caseSourceDocumentRecordMapper.selectByExample(example);
            if (ObjectUtil.isNotEmpty(caseSourceDocumentRecordList)) {
                for (CaseSourceDocumentRecord caseSourceDocumentRecord : caseSourceDocumentRecordList) {
                    CaseTransferRecordFile caseTransferRecordFile = new CaseTransferRecordFile();
                    caseTransferRecordFile.setCaseSourceId(caseId);
                    caseTransferRecordFile.setFileName(caseSourceDocumentRecord.getDocumentNumber());
                    caseTransferRecordFile.setFileUrl(caseSourceDocumentRecord.getDocumentUrl());
                    caseTransferRecordFile.setTransferType(Constant.STRING_3);
                    caseTransferRecordFile.setEvidenceCata(Constant.STRING_2);
                    caseTransferRecordFile.setFileType("pdf");
                    caseTransferRecordFile.setFileSuffix(".pdf");
                    caseTransferRecordFileList.add(caseTransferRecordFile);
                }
            }
        }

        //证据列表
        List<Map> evidenceDetailList = evidenceInventoryService.getEvidenceDetailList(caseId);
        if (ObjectUtil.isNotEmpty(evidenceDetailList)) {
            for (Map evidenceDetail : evidenceDetailList) {
                //修改为全部取证据文件信息
                if (ObjectUtil.isNotEmpty(evidenceDetail.get("evidenceFileList"))) {
                    List<Map> evidenceFileList = JSON.parseArray(JSON.toJSONString(evidenceDetail.get("evidenceFileList")), Map.class);
                    for (Map evidenceFile : evidenceFileList) {
                        //证据名称+"-"+文件名称
                        String fileName = evidenceDetail.get("evidenceName").toString() + "-" + evidenceFile.get("fileName").toString();

                        CaseTransferRecordFile caseTransferRecordFile = new CaseTransferRecordFile();
                        caseTransferRecordFile.setCaseId(caseId);
                        caseTransferRecordFile.setCaseSourceId(caseId);
                        caseTransferRecordFile.setFileName(fileName);
                        caseTransferRecordFile.setFileUrl(evidenceFile.get("fileUrl").toString());
                        caseTransferRecordFile.setTransferType(transferType);
                        caseTransferRecordFile.setEvidenceCata(Constant.STRING_1);
                        caseTransferRecordFile.setFileType(evidenceFile.get("fileSuffix").toString());
                        caseTransferRecordFile.setFileSuffix("." +evidenceFile.get("fileSuffix").toString());

                        caseTransferRecordFileList.add(caseTransferRecordFile);
                    }
                }
            }
        }

        return caseTransferRecordFileList;
    }

    /**
     * 校验案源移交时的关联对象
     *
     * @param caseSourceId 案源id
     * @return com.icinfo.framework.common.web.Result<Boolean>
     * @author liuyongfei
     * @date 2022/12/15
     */
    @Override
    public Result<Boolean> checkSourceTransferParties(String caseSourceId) {
        List<CasePartyVo> casePartyVoList = getCasePartyVoList(Constant.STRING_3, caseSourceId);
        return Result.success(CollectionUtils.isNotEmpty(casePartyVoList));
    }

    /**
     * 修改主案件状态
     *
     * @param caseId 案件id
     * @param status 状态
     * @author liuyongfei
     * @date 2022/12/08
     */
    private void updateCaseStatus(String caseId, String status) {
        CaseMainInfo caseMainInfo = new CaseMainInfo();
        caseMainInfo.setId(caseId);
        caseMainInfo.setCaseStatus(status);
        caseMainInfo.setCaseCurrentStageCode(CaseStageEnum.CLOSE_ARCHIVING.getCode());
        caseMainInfo.setCaseCurrentStageName(CaseStageEnum.CLOSE_ARCHIVING.name());
        caseMainInfo.setCaseCurrentLinkName("归档");
        caseMainInfo.setCaseCurrentLinkCode("LINK-" + DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        caseMainInfoMapper.updateByPrimaryKeySelective(caseMainInfo);
    }

    /**
     * 查询案件主表信息
     *
     * @param caseId 案件id
     * @return com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo
     * @author liuyongfei
     * @date 2022/12/09
     */
    private CaseMainInfo getCaseMainInfo(String caseId) {
        return caseMainInfoMapper.selectByPrimaryKey(caseId);
    }

    /**
     * 构造地址信息
     *
     * @param casePartyVo 要构造的事人对象
     * @param type        类型
     * @param id          当事人id
     * @author liuyongfei
     * @date 2022/12/09
     */
    private void buildCaseParty(CasePartyVo casePartyVo, String type, String id) {

        //自然人
        if (Constant.STRING_1.equals(type)) {
            CasePartiesPersonInfo casePartiesPersonInfo = casePartiesPersonInfoMapper.selectByPrimaryKey(id);
            if (Objects.nonNull(casePartiesPersonInfo)) {
                casePartyVo.setAddress(casePartiesPersonInfo.getAddress());
            }
        } else {
            //法人
            CasePartiesLegalInfo casePartiesLegalInfo = casePartiesLegalInfoMapper.selectByPrimaryKey(id);
            if (Objects.nonNull(casePartiesLegalInfo)) {
                casePartyVo.setAddress(casePartiesLegalInfo.getAddress());
            }
        }
    }

    /**
     * 构造条件参数
     *
     * @param caseTransferRecordDto 移交记录入参对象
     * @return com.icinfo.framework.mybatis.mapper.entity.Example
     * @author liuyongfei
     * @date 2022/12/12
     */
    private Example buildExample(CaseTransferRecordDto caseTransferRecordDto) {
        Example example = new Example(CaseTransferRecord.class);
        if (Constant.STRING_1.equals(caseTransferRecordDto.getTransferType())) {
            example.createCriteria().andEqualTo("caseId", caseTransferRecordDto.getCaseId())
                    .andEqualTo("transferType", caseTransferRecordDto.getTransferType())
                    .andEqualTo("sendUid", caseTransferRecordDto.getSendUid())
                    .andEqualTo("deptRelUid", caseTransferRecordDto.getDeptRelUid());
        } else if (Constant.STRING_2.equals(caseTransferRecordDto.getTransferType())) {
            example.createCriteria().andEqualTo("caseId", caseTransferRecordDto.getCaseId())
                    .andEqualTo("transferType", caseTransferRecordDto.getTransferType())
                    .andEqualTo("sendUid", caseTransferRecordDto.getSendUid());
        } else {
            example.createCriteria().andEqualTo("caseSourceId", caseTransferRecordDto.getCaseSourceId())
                    .andEqualTo("transferType", caseTransferRecordDto.getTransferType())
                    .andEqualTo("sendUid", caseTransferRecordDto.getSendUid());
        }
        return example;
    }

    /**
     * 更新案源主表状态
     *
     * @param caseSourceId 案源id
     * @param status       状态值
     * @author liuyongfei
     * @date 2022/12/14
     */
    private void updateCaseSourceStatus(String caseSourceId, String status) {
        CaseSourceRegisterMainInfo caseSourceRegisterMainInfo = new CaseSourceRegisterMainInfo();
        caseSourceRegisterMainInfo.setId(caseSourceId);
        caseSourceRegisterMainInfo.setCaseSourceStatus(status);
        caseSourceRegisterMainInfo.setCaseSourceStageCode(Constant.CASE_SOURCE_STAGE_AYYS);
        caseSourceRegisterMainInfoMapper.updateByPrimaryKeySelective(caseSourceRegisterMainInfo);
    }

    /**
     * 获取关联对象
     *
     * @param transferType 移交类型：1-协查协办，2-案源移交，3-案件移交
     * @param id           案件/案源 id
     * @return java.util.List<com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.vo.CasePartyVo>
     * @author liuyongfei
     * @date 2022/12/14
     */
    private List<CasePartyVo> getCasePartyVoList(String transferType, String id) {
        List<CasePartyVo> casePartyVoList = new ArrayList<>();
        if (Constant.STRING_3.equals(transferType)) {
            //案源关联对象
            List<CaseSourcePartyInfoVo> caseSourcePartyInfo =
                    caseSourcePartyInfoService.getCaseSourcePartyInfo(id);
            for (CaseSourcePartyInfoVo caseSourcePartyInfoVo : caseSourcePartyInfo) {
                CasePartyVo casePartyVo = new CasePartyVo();
                BeanUtil.copyProperties(caseSourcePartyInfoVo, casePartyVo);
                casePartyVoList.add(casePartyVo);
            }
        } else {
            //案件关联对象
            List<CasePartiesRecordVo> casePartiesInfoList = casePartiesRecordService.getCasePartiesInfo(id);
            if (CollectionUtils.isNotEmpty(casePartiesInfoList)) {
                for (CasePartiesRecordVo casePartiesRecordVo : casePartiesInfoList) {
                    //根据类型查询住址
                    CasePartyVo casePartyVo = new CasePartyVo();
                    BeanUtil.copyProperties(casePartiesRecordVo, casePartyVo);
                    buildCaseParty(casePartyVo, casePartiesRecordVo.getPartyType(), casePartiesRecordVo.getPartyId());
                    casePartyVoList.add(casePartyVo);
                }
            }
        }
        return casePartyVoList;
    }
}